package com.app.controller.supermarket;

import com.app.common.EducationTool;

import com.app.mapper.CusEducationMapper;
import com.app.mapper.supermarket.*;
import com.app.model.supermarket.*;
import com.app.service.CusEducationService;
import com.app.service.supermarket.*;
import com.app.util.BizException;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.supermarket.OddJobReleaseReturnVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 人员评论控制层
 * @Date: 2022/10/15 15:47
 */
@RestController
@RequestMapping("api/comment")
public class CommentController {
    @Autowired
    private MktPersonRepository mktPersonRepository;
    @Autowired
    private CommentService commentService;
    @Autowired
    private BaseMappers baseMappers;
    @Autowired
    private MktPersonService mktPersonService;
    @Autowired
    SkillSonService skillSonService;
    @Autowired
    private MyOddService myOddService;
    @Autowired
    private OddJobReleaseService oddJobReleaseService;
    @Autowired
    private MktCertService mktCertService;
    @Autowired
    private CusEducationService cusEducationService;
    @ApiOperation(value = "人员评论列表")
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public String getList(@RequestParam("oid") int oid, @RequestParam("mid") int mid, HttpServletRequest request) {
        String token = request.getHeader("token");
        JSONObject json = new JSONObject();
        if(token==null)
        {
            json.put("code", 0);
            json.put("msg", "token不存在");
            return json.toString();
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel user = mktPersonService.getModelByMobile(mobile);
        if(user==null)
        {
            json.put("code", 0);
            json.put("msg", "用户不存在");
            return json.toString();
        }
        int exi = baseMappers.exists(mid);
        if (exi == 1) {
            MktPersonQueryParamModel mktPersonVo = mktPersonRepository.selectById(mid);
            QueryWrapper<CommentModel> queryWrapper = new QueryWrapper<>();
            QueryWrapper<SkillSonModel> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("mid", mktPersonVo.getId());
            queryWrapper.eq("mid", mktPersonVo.getId());
            List<CommentModel> comments = commentService.getCommentList(mid);
            for (int i = 0; i < comments.size(); i++) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm ");
                Timestamp data1 = Timestamp.valueOf(comments.get(i).getCrtTime());
                String StrCrtTime = sdf.format(data1);
                comments.get(i).setCrtTime(StrCrtTime);
            }

            mktPersonVo.setCommentList(comments);
            MktPersonModel  model= mktPersonService.getPersonByMid(mid);
            if(model.getSkills() == null && model.getSkills() == "" || model.getSkills().length() <= 0){
                mktPersonVo.setSkillSons(null);

            }else { //查询标签
                String skillsString = model.getSkills().substring(0, model.getSkills().length()-0 );
                String[] result = skillsString.split(",");
                List<SkillSonModel> skillSonList = new ArrayList<>();
                for (int i = 0; i < result.length; i++) {
                    SkillSonModel son  = skillSonService.getSkillSon(Integer.parseInt(result[i]));
                    skillSonList.add(son);
                }
                mktPersonVo.setSkillSons(skillSonList);}

            List<MyOddModel> modelList = myOddService.getModelByPid(mid,oid);

            if (model != null) {

                JSONObject job = new JSONObject(mktPersonVo);

                if(mktPersonVo.getSkillSons()==null)
                {

                    job.put("skillSons","");
                }
                //学历
                String eduName = "";
                if (mktPersonVo.getEducation() != null ) {
                    job.put("education","暂无学历");
                }
                //学历条件筛选
                if (mktPersonVo.getEducation() != null ) {
                    int toUp = 0;
                    for (int k = 0; k < EducationTool.EDUCATION_VO_LIST.size(); k++) {
                        if (Integer.parseInt(mktPersonVo.getEducation()) == EducationTool.EDUCATION_VO_LIST.get(k).getId()) {
                            eduName = EducationTool.EDUCATION_VO_LIST.get(k).getData();
                        }
                    }
                    mktPersonVo.setEducation(eduName);
                } else {
                    mktPersonVo.setEducation("暂无学历");
                }
                job.put("name",mktPersonVo.getName());
                job.put("education",mktPersonVo.getEducation());
                job.put("experience",mktPersonVo.getExperience());
                job.put("sex",mktPersonVo.getSex());

                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH: mm ");
                Timestamp data1 = Timestamp.valueOf(mktPersonVo.getCrtTime());
                String StrCrtTime = sdf.format(data1);
                mktPersonVo.setCrtTime(StrCrtTime);

//                phone.substring(0, 3) + "****" + phone.substring(7)  星号电话

                String starPhone = user.getStarPhone().substring(0,3) +"****" + user.getStarPhone().substring(7) ;

                int OddCount = myOddService.getCountByOidAndState(Integer.valueOf(oid));
                int OddJobCount = oddJobReleaseService.selectNumber(Integer.valueOf(oid));
                MyOddModel oddModel = myOddService.getModelByMidAndOid(mid,oid);
                if(OddCount == OddJobCount){
                    json.put("jobState","8");//录满
                }else {
                    json.put("jobState","9");//未录满
                }
                //录用状态
                if(modelList != null &&  modelList.size() > 0){
                    if(modelList.get(0).getState() > 1 && modelList.get(0).getState() < 6 ){
                        json.put("upState", 1);//已录用
                    }else {
                        json.put("upState", 2);//未录用
                    }
                }else {
                    json.put("upState", 2);
                }
               List<MktCertModel> mktCertList = mktCertService.getListByMid(mid);
                job.put("mktCertList",mktCertList);//查图集
                job.put("crtTime",oddModel.getCrtTime());
                job.put("mobile",model.getTellPhone());
                job.put("starPhone",starPhone);
                job.put("star",model.getStar());
                json.put("data", job);
                json.put("code", "1");
                json.put("msg", "成功");
            }
        }
        return json.toString();
    }

    @ApiOperation(value = "提交评价")
    @PostMapping("/addComment")
    public Result<Object> addComment(@RequestParam("star") Integer star,
                             @RequestParam("content") String content,
                             HttpServletRequest request) {
        String token = request.getHeader("token");
        if(token==null)
        {
            return Result.error(405,"token不存在");
        }
        String mobile = TokenUtils.verify(token);
        MktPersonModel mktPerson = mktPersonService.getModelByMobile(mobile);
        if(mktPerson==null)
        {
            return Result.error(405,"用户不存在");
        }
        CommentModel comment = new CommentModel();
        comment.setFaceUrl(mktPerson.getFaceUrl());
        comment.setName(mktPerson.getName());
        comment.setMid(mktPerson.getId());
        comment.setStar(star);
        comment.setContent(content);
        commentService.insert(comment);
        return Result.success("操作成功");
    }
}
